/**
 * @filename:FilesServiceImpl 2022-04-24 10:49:28
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2018 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwl.blue.commont.base.BizException;
import com.lwl.blue.commont.base.ErrorCode;
import com.lwl.blue.commont.base.JsonResult;
import com.lwl.blue.commont.base.PageParam;
import com.lwl.blue.dao.FilesDao;
import com.lwl.blue.entity.Files;
import com.lwl.blue.service.FilesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

/**
 * <p>自定义serviceImpl写在这里</p>
 *
 * <p>说明： 上传文件记录表服务实现层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@Service
public class FilesServiceImpl extends ServiceImpl<FilesDao, Files> implements FilesService {
    @Resource
    FilesDao filesDao;

    @Override
    public JsonResult<Page<Files>> pages(PageParam<Files> pageParam) {
        Page<Files> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        return new JsonResult<Page<Files>>().success(filesDao.pages(page, pageParam.getParam()));

    }

    @Override
    public void updateBySaveOrUpdate(String urls, String oldUrls, Integer sourceId, String itemNo) {
        //减少权重
        if (StringUtils.isNotBlank(oldUrls)) {
            String[] split1 = oldUrls.split(";");
            if (split1.length > 0) {
                for (String s : split1) {
                    if (StringUtils.isNotBlank(s)) {
                        if (-1 != urls.indexOf(s)) {
                            urls = urls.replaceAll(s + ";", "");
                            continue;
                        }
                        Files file = this.getOne(new QueryWrapper<Files>().lambda()
                                .eq(Files::getUrl, s), false);
                        if (Objects.nonNull(file)) {
                            file.setWeight(file.getWeight() - 1);
                            file.setUpdateTime(new Date());
                            this.updateById(file);
                        }
                    }
                }
            }
        }

        //数据加权未处理
        if (StringUtils.isNotBlank(urls)) {
            String[] split = urls.split(";");
            //增加权重
            if (split.length > 0) {
                Arrays.stream(split).forEach(s -> {
                    Files file = this.getOne(new QueryWrapper<Files>().lambda()
                            .eq(Files::getUrl, s), false);
                    if (Objects.nonNull(file)) {
                        if (Objects.isNull(file.getSourceId())) {
                            file.setSourceId(sourceId);
                            file.setItemNo(itemNo);
                        }
                        file.setWeight(file.getWeight() + 1);
                        file.setUpdateTime(new Date());
                        this.updateById(file);
                    } else {
                        throw new BizException(ErrorCode.LOGIN_FILE_EXISTS_ERROR.getCode(), ErrorCode.LOGIN_FILE_EXISTS_ERROR.getMsg());
                    }
                });
            }
        }
    }

    @Override
    public void updateByDelete(String urls) {
        //减少权重
        if (StringUtils.isNotBlank(urls)) {
            String[] split1 = urls.split(";");
            if (split1.length > 0) {
                for (String s : split1) {
                    if (StringUtils.isNotBlank(s)) {
                        Files file = this.getOne(new QueryWrapper<Files>().lambda()
                                .eq(Files::getUrl, s), false);
                        if (Objects.nonNull(file)) {
                            file.setWeight(file.getWeight() - 1);
                            file.setUpdateTime(new Date());
                            this.updateById(file);
                        }
                    }
                }
            }
        }
    }


}
